﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
using PhilipsChannelOrder.ViewModels;
using PhilipsChannelOrder.Models;
using System.Collections;
using System.Linq;

namespace PhilipsChannelOrderTest
{
    [TestClass]
    public class ChannelOrderTest
    {

        public System.IO.Stream GetXML()
        {
            Assembly thisAssembly = Assembly.GetExecutingAssembly();
            string path = "PhilipsChannelOrderTest.test.xml";
            return thisAssembly.GetManifestResourceStream(path);
        }

        private ChangeOrderViewModel changeOrder;

        [TestInitialize]
        public void InitilizeTests()
        {
            changeOrder = new ChangeOrderViewModel();
            var xmlStream = GetXML();
            var channelList = changeOrder.ParseFile(xmlStream);
            changeOrder.ChannelMap = new System.Collections.ObjectModel.ObservableCollection<PhilipsChannelOrder.Models.ChannelNode>(channelList);
        }

        [TestMethod]
        public void ReadFile()
        {
            var channelList = changeOrder.ChannelMap.AsEnumerable();
            Assert.AreEqual(10, channelList.Count());
            int last = -1;
            foreach (var channel in channelList)
            {
                Assert.IsTrue(last < channel.PresetNumber);
                last = channel.PresetNumber;
            }
        }

        [TestMethod]
        public void ChannelUp()
        {
            ChannelNode channel = changeOrder.ChannelMap[4];
            string channelName = channel.Name;
            Assert.AreEqual(changeOrder.ChannelMap[3].OldPresetNumber, changeOrder.ChannelMap[3].PresetNumber);
            Assert.AreEqual(changeOrder.ChannelMap[4].OldPresetNumber, changeOrder.ChannelMap[4].PresetNumber);

            changeOrder.ChannelUp(channel);

            Assert.AreEqual(changeOrder.ChannelMap[4].OldPresetNumber, changeOrder.ChannelMap[3].PresetNumber);
            Assert.AreEqual(changeOrder.ChannelMap[3].OldPresetNumber, changeOrder.ChannelMap[4].PresetNumber);
            Assert.AreEqual(channelName, changeOrder.ChannelMap[3].Name);
        }

        [TestMethod]
        public void ChannelDown()
        {
            ChannelNode channel = changeOrder.ChannelMap[3];
            string channelName = channel.Name;
            Assert.AreEqual(changeOrder.ChannelMap[3].OldPresetNumber, changeOrder.ChannelMap[3].PresetNumber);
            Assert.AreEqual(changeOrder.ChannelMap[4].OldPresetNumber, changeOrder.ChannelMap[4].PresetNumber);

            changeOrder.ChannelDown(channel);

            Assert.AreEqual(changeOrder.ChannelMap[4].OldPresetNumber, changeOrder.ChannelMap[3].PresetNumber);
            Assert.AreEqual(changeOrder.ChannelMap[3].OldPresetNumber, changeOrder.ChannelMap[4].PresetNumber);
            Assert.AreEqual(channelName, changeOrder.ChannelMap[4].Name);
        }

        [TestMethod]
        public void MoveToEnd()
        {
            var channel = changeOrder.ChannelMap[4];
            changeOrder.MoveToEnd(channel);
            Assert.AreSame(channel, changeOrder.ChannelMap.Last());
        }

        [TestMethod]
        public void UpdateOrderUp()
        {
            ChannelNode channel = changeOrder.ChannelMap[3];
            Assert.AreEqual(4, changeOrder.ChannelMap[3].PresetNumber);
            Assert.AreEqual(8, changeOrder.ChannelMap[7].PresetNumber);
            changeOrder.UpdateOrder(channel, "8\r\n");

            Assert.AreEqual(4, changeOrder.ChannelMap[3].PresetNumber);
            Assert.AreEqual(8, changeOrder.ChannelMap[7].PresetNumber);
            Assert.AreEqual(5, changeOrder.ChannelMap[3].OldPresetNumber);
            Assert.AreEqual(4, changeOrder.ChannelMap[7].OldPresetNumber);
        }

        [TestMethod]
        public void UpdateOrderDown()
        {
            ChannelNode channel = changeOrder.ChannelMap[7];
            Assert.AreEqual(4, changeOrder.ChannelMap[3].PresetNumber);
            Assert.AreEqual(8, changeOrder.ChannelMap[7].PresetNumber);
            changeOrder.UpdateOrder(channel, "4\r\n");

            Assert.AreEqual(4, changeOrder.ChannelMap[3].PresetNumber);
            Assert.AreEqual(8, changeOrder.ChannelMap[7].PresetNumber);
            Assert.AreEqual(8, changeOrder.ChannelMap[3].OldPresetNumber);
            Assert.AreEqual(7, changeOrder.ChannelMap[7].OldPresetNumber);
        }

        [TestMethod]
        public void RemoveEmptyChannels()
        {
            changeOrder.ChannelMap[4].Name = "-----";
            changeOrder.RemoveEmptyChannels();
            Assert.AreEqual(9, changeOrder.ChannelMap.Count);
            Assert.AreEqual(6, changeOrder.ChannelMap[4].OldPresetNumber);
            Assert.AreEqual(5, changeOrder.ChannelMap[4].PresetNumber);
        }

    }
}
